package com.tairan.chapter.flowable.service.ext.impl;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.idm.api.User;
import org.flowable.idm.engine.impl.UserQueryImpl;
import org.flowable.idm.engine.impl.persistence.entity.UserEntity;
import org.flowable.idm.engine.impl.persistence.entity.UserEntityImpl;

import java.util.ArrayList;
import java.util.List;

public class FlowUserQueryImpl extends UserQueryImpl {

    private static final long serialVersionUID = -2196307416653428197L;

    @Override
    public long executeCount(CommandContext commandContext) {
        return executeQuery().size();
    }

    @Override
    public List<User> executeList(CommandContext commandContext) {
        return executeQuery();
    }

    protected List<User> executeQuery() {
        if (null != getId()) {
            List<User> result = Lists.newArrayList();
            result.add(findById(getId()));
            return result;
        } else if (null != getIdIgnoreCase()) {
            List<User> result = Lists.newArrayList();
            result.add(findById(getIdIgnoreCase()));
            return result;
        } else {
            return executeAllUserQuery();
        }
    }

    protected UserEntity findById(String userId) {
        UserEntity userEntity = new UserEntityImpl();

        if ("litairan".equals(userId)) {
            userEntity.setId("litairan");
            userEntity.setFirstName("部门经理");
            userEntity.setLastName(StringUtils.EMPTY);
            userEntity.setPassword("57b89d1421898c692ebf98b530c3869b56d32e1e80f2bb703aba8a39");
            userEntity.setEmail("15036900300@163.com");
            userEntity.setRevision(1);
        } else if("zhanghao".equals(userId)) {
            userEntity.setId("zhanghao");
            userEntity.setFirstName("人力资源");
            userEntity.setLastName(StringUtils.EMPTY);
            userEntity.setPassword("57b89d1421898c692ebf98b530c3869b56d32e1e80f2bb703aba8a39");
            userEntity.setEmail("zhanghao@163.com");
            userEntity.setRevision(1);
        }
        return userEntity;
    }

    protected List<User> executeNameQuery(String name) {
        String fullName = name.replaceAll("%", "");
        return executeUsersQuery(fullName);
    }

    protected List<User> executeAllUserQuery() {
        return executeUsersQuery("");
    }

    protected List<User> executeUsersQuery(String fullName) {
        return Lists.newArrayList();
    }
}
